package com.zhiyou100.api.list_demo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Demo3 {
    /**
     * 使用 ArrayList 类向集合中添加三个商品信息，包括商品编号、名称和价格，
     * 然后根据商品价格从高到低输出商品信息
     */

    public static void main(String[] args) {
        List<Product> productList = new ArrayList<>();
        productList.add(new Product("001","手机",5999.9));
        productList.add(new Product("002","电脑",6999.9));
        productList.add(new Product("003","pad",3999.9));
        productList.add(new Product("004","鼠标",99.9));
        //对集合中的元素排序
        productList.sort(new Comparator<Product>(){
            @Override
            public int compare(Product o1, Product o2) {
                Double price1 = o1.getPrice();
                Double price2 = o2.getPrice();
                return price2.compareTo(price1);
            }
        });
        for (Product product : productList) {
            System.out.println(product);
        }
    }
}

/**
 * 商品实体类
 */
class Product{
    private String num;  //商品编号
    private String name; //商品名称
    private double price; //商品价格

    public Product() {
    }

    public Product(String num, String name, double price) {
        this.num = num;
        this.name = name;
        this.price = price;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "num='" + num + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
